package com.m4f.city.business.domain;

import java.io.Serializable;
import java.util.Collection;
import java.util.List;

import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.IdentityType;
import javax.jdo.annotations.NotPersistent;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;

import org.springframework.security.core.GrantedAuthority;

import com.google.api.client.util.Key;
import com.m4f.city.utils.serializer.annotation.M4FPropertySerializable;
import com.m4f.city.utils.serializer.annotation.M4FSerializable;
import com.m4f.gaeweb.model.GaeM4FUser;

@PersistenceCapable(identityType = IdentityType.APPLICATION, detachable="true")
@M4FSerializable(name = "user")
public class InternalUser implements Serializable, GaeM4FUser  {
	/**
	 * 
	 */
	private static final long serialVersionUID = 6040885036655102340L;

	@PrimaryKey  
	@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
	@Key("id") // From GenericJson
	@M4FPropertySerializable(name="id")
	private Long id;
	
	@Persistent
	//message = "{email.blank}"
	@NotNull(message="Mail blank")
	//@Pattern(regexp = "^[\\w-\\.]+@([\\w-]+\\.)+[\\w-]{2,4}$", message="Bad mail")
	@M4FPropertySerializable(name="email")
	private String email;
	
	@Persistent
	@NotNull(message="Bad Password")
	@Size(min=6, max=25)
	//@Min(message = "{password.error}", value=6)
	private String password;
	
	@Persistent
	@NotNull(message="Name blank")
	@Key("name")
	private String name; 
	
	@Persistent
	@M4FPropertySerializable(name="surname")
	private String surname; 
	
	@Persistent
	private String nickname; 
	
	// TODO AVATAR
	
	@Persistent
	@M4FPropertySerializable(name="organization")
	private Long organizationId; 
	
	@Persistent
	@NotNull(message="Yes or Not is admin")
	private Boolean admin;
	
	@Persistent  
	private Boolean active = Boolean.TRUE;
	
	@NotPersistent
	Collection<GrantedAuthority> authorities;
	
	@Persistent
	@M4FPropertySerializable(name="applications")
	private List<Long> applicationIds; 
		
	public void setId(Long id) {
		this.id = id;
	}
	
	public Long getId() {
		return id;
	}
	
	public String getEmail() {
		return email;
	}
	public void setEmail(String email) {
		this.email = email;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}

	public Boolean getAdmin() {
		return admin;
	}
	
	/*public Boolean isAdmin() {
		return admin;
	}*/
	public void setAdmin(Boolean admin) {
		this.admin = admin;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getName() {
		return name;
	}

	@Override
	public Collection<GrantedAuthority> getAuthorities() {
		return this.authorities;
	}
	
	public void setAuthorities(Collection<GrantedAuthority> authorities) {
		this.authorities = authorities;
	}

	@Override
	public String getUsername() {
		return email;
	}

	@Override
	public boolean isAccountNonExpired() {
		return true;
	}

	@Override
	public boolean isAccountNonLocked() {
		return true;
	}

	@Override
	public boolean isCredentialsNonExpired() {
		return true;
	}

	@Override
	public boolean isEnabled() {
		return this.active != null ? this.active.booleanValue() : true;
	}

	public String getSurname() {
		return surname;
	}

	public void setSurname(String surname) {
		this.surname = surname;
	}

	public String getNickname() {
		return nickname;
	}

	public void setNickname(String nickname) {
		this.nickname = nickname;
	}

	public Boolean getActive() {
		return active;
	}

	public void setActive(Boolean active) {
		this.active = active;
	}

	public Long getOrganizationId() {
		return organizationId;
	}

	public void setOrganizationId(Long organizationId) {
		this.organizationId = organizationId;
	}

	public List<Long> getApplicationIds() {
		return applicationIds;
	}

	public void setApplicationIds(List<Long> applicationIds) {
		this.applicationIds = applicationIds;
	}
}